package com.example.mn_mvvm_demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.lib_cache.view_model.MainActivityViewModel;
import com.example.lib_common.base.BaseActivity;
import com.example.mn_mvvm_demo.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends BaseActivity implements
        NavigationView.OnNavigationItemSelectedListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    ActivityMainBinding activityMainBinding;
    MainActivityViewModel mainActivityViewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);

         activityMainBinding =
                DataBindingUtil.setContentView(this,
                        R.layout.activity_main);

         //绑定声明周期
        activityMainBinding.setLifecycleOwner(this);
        mainActivityViewModel=new MainActivityViewModel();
        activityMainBinding.setViewModel(mainActivityViewModel);


        initView();
    }

    private void initView() {
        //DrawerLayout和Toolbar 关联

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,
                activityMainBinding.dl,
                activityMainBinding.include.toolbar,
                R.string.open,
                R.string.close);
        actionBarDrawerToggle.syncState();

        //添加抽屉监听
        activityMainBinding.dl.addDrawerListener(actionBarDrawerToggle);
        //注册抽屉栏里面的点击事件
        activityMainBinding.navigationView
                .setNavigationItemSelectedListener(this);


        Fragment fragmentContainer = getSupportFragmentManager().findFragmentById(R.id.nav_fragment);
        final NavController navController = NavHostFragment.findNavController(fragmentContainer);
        onPrepareOptionsMenu(activityMainBinding.navButtom.getMenu());

        //给底部导航栏设置点击事件
        activityMainBinding.navButtom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                navController.navigate(menuItem.getItemId());
                return !TextUtils.isEmpty(menuItem.getTitle());
            }
        });

        NavigationUI.setupWithNavController(activityMainBinding.navButtom,navController);
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        Log.e(TAG, "onNavigationItemSelected: "+String.valueOf(menuItem.getTitle()));
        String title = (String) menuItem.getTitle();
        Toast.makeText(this, "点击了----- " + title, Toast.LENGTH_SHORT).show();

        activityMainBinding.dl.close();
        return false;
    }
}